| No. | Answer | Remark | |
|---|---|---|---|
| 1 | c | Prints: v y w z | The legal types for the switch expression are byte, short, char, and int. The constant case expressions can be any expression that is assignable to the type of the switch expression. 'd'-'a'=3. ~0=-1. 4&5=4. |
| 2 | f | Compiler Error | The switch expression can not be of type long. The legal types for the switch expression are byte, short, char, and int. |
| 3 | a | Prints: 12457 | Since each iteration of the loop prints the value of the switch expression, it should be possible to figure out what is going on here. If more information is needed, then change the print statement so that the values of i and j are printed separately in columns. |
| 4 | g | Compiler Error | In the C programming language, the boolean value of false is represented by zero and true is any non zero value. In the Java programming language, numeric values are not assignable to a boolean value. The use of the numeric value 1 as the boolean expression in the while loop causes a compiler error. |
| 5 | e | Prints: 2, 1, 3, 1 | A break statement with a label will transfer control outside of the enclosing statement with the matching label. Furthermore, the enclosing statements complete abruptly. In the case of a do-while loop, the boolean expression that controls the loop is not evaluated as control transfers out of the loop. However, if the break statement transfers control out of a try-catch-finally statement, then the finally clause is processed. |
| 6 | d | Prints: 3, 5, 9, | A continue statement without a label will cause the current iteration of the enclosing loop to end and the next iteration to begin. As a result, the boolean expression that controls the loop is evaluated as the next iteration begins. Similarly, a continue statement that has a label transfers control to the next iteration of the enclosing loop that has the same label. If the continue statement causes control to break out of a nested loop to transfer control to an outer loop with a matching label, then execution of the nested loop is completed abruptly as control transfers to the outer loop. Furthermore, the boolean expression that controls the nested loop is not evaluated as control is abruptly transferred to the labeled outer loop. |
| 7 | d | Prints: 3, 6, 12, | A break statement with a label will transfer control outside of the enclosing statement with the matching label. Furthermore, the enclosing statements complete abruptly. In the case of a do-while loop, the boolean expression that controls the loop is not evaluated as control transfers out of the loop. However, if the break statement transfers control out of a try-catch-finally block, then the finally clause is processed. The first iteration of this code example executes the case statement with the value 3. As a result, the statement "break label3" is executed causing the inner most loop to complete abruptly and control to be transferred out of the inner most loop to the second nested loop. The boolean expression of the second loop is evaluted and variable j is incremented as a side effect. The boolean expression evalutes as true and a second iteration begins. Variable j is incremented again at the top of the loop and varible i is incremented at the top of the inner most loop. The switch expression evaluates as six. A continue statement without a label will cause the current iteration of the enclosing loop to end and the next iteration to begin. As a result, the boolean expression that controls the loop is evaluated as the next iteration begins. Similarly, a continue statement that has a label transfers control to the next iteration of the enclosing loop that has the same label. If the continue statement causes control to break out of a nested loop to transfer control to an outer loop with a matching label, then execution of the nested loop is completed abruptly as control transfers to the outer loop. Furthermore, the boolean expression that controls the nested loop is not evaluated as control is abruptly transferred to the labeled outer loop. |